package com.example.dairenkun.neufoodshop.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.example.dairenkun.neufoodshop.R;
import com.example.dairenkun.neufoodshop.activity.FoodActivity;
import com.example.dairenkun.neufoodshop.bean.Collection;
import com.example.dairenkun.neufoodshop.bean.Shop;
import com.example.dairenkun.neufoodshop.model.ShopModel;
import com.squareup.picasso.Picasso;

import java.util.List;

;import retrofit2.Call;
import retrofit2.Callback;


public class CollectShopAdapter extends RecyclerView.Adapter<ViewHolder> {
    private Context mContext;
    private List mDataList;
    private LayoutInflater mLayoutInflater;
    public CollectShopAdapter(Context mContext, List mDataList){
        this.mContext = mContext;
        this.mDataList = mDataList;
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = mLayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_collectshop, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final Collection entity = (Collection) mDataList.get(position);
        if (null == entity)
            return;
        ViewHolder viewHolder = (ViewHolder) holder;
        viewHolder.shop_name.setText(entity.getShopname());
        viewHolder.shop_site.setText(entity.getAddress());
        if (!"".equals(entity.getPic()))
        Picasso.with(mContext).load(entity.getPic()).into(viewHolder.shop_image);

         ;


        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                  int shopid=entity.getShop_id();
                ShopModel shopModel=new ShopModel();
                Call <Shop> call=shopModel.ShopDetail(shopid);

                call.enqueue(new Callback<Shop>() {
                    @Override
                    public void onResponse(Call<Shop> call, retrofit2.Response<Shop> response)
                    {
                        final Shop shop=response.body();

                        Intent intent=new Intent(mContext,FoodActivity.class);
                        intent.putExtra("shop_id",shop.getShop_id()+"");
                        intent.putExtra("shopname",shop.getShopname());
                        intent.putExtra("shopphone",shop.getPhonenum());
                        intent.putExtra("shopintro",shop.getIntro());
                        intent.putExtra("shopstar",shop.getLevel()+"");
                        mContext.startActivity(intent);
                    }
                    @Override
                    public void onFailure(Call<Shop> call, Throwable t) {

                    }
                });


            }
        });
    }


    @Override
    public int getItemCount() {
        return mDataList.size();
    }
    public class ViewHolder extends RecyclerView.ViewHolder{
        TextView shop_name;
        ImageView shop_image;
        TextView shop_site;


        public ViewHolder(View itemView) {
            super(itemView);
            shop_name = (TextView) itemView.findViewById(R.id.Collectshop_name);
            shop_image = (ImageView) itemView.findViewById(R.id.Collectshop_image);
            shop_site = (TextView) itemView.findViewById(R.id.Collectshop_site);


        }
    }


}
